home *** CD-ROM | disk | FTP | other *** search
- Path: argonet.co.uk!argbe15
- From: Dave Mullard <dmullard@argonet.co.uk>
- Newsgroups: comp.lang.c++
- Subject: Re: How to get at base class fields from member functions?
- Date: Sun, 17 Mar 1996 20:03:46
- Organization: UnipalmPIPEX server (post doesn't reflect views of UnipalmPIPEX
- Distribution: world
- Message-ID: <internews46B0766104@argonet.co.uk>
- References: <internews46B00D1FBD@argonet.co.uk> <4ihhkf$2ni@news5.erols.com>
- Reply-To: Dave Mullard <dmullard@argonet.co.uk>
- NNTP-Posting-Host: ai213.du.pipex.com
- X-Newsreader: VTi Voyager InterNews 0.15 for Acorn RISC OS (Patched by RA)
-
- Chris Cobb <ccobb@erols.com> wrote:
- > I <dmullard@argonet.co.uk> wrote:
- > >Given three classes A, B and C I want to have multiple As for each B
- > and
- > >multiple Bs for each C. To do this I could create the following.
- > >
- > >class b1 : public B
- > >{
- > >A a1;
- > >A a2;
- > >A a3;
- > >};
-
- > >class b2 : public B
- > >{
- > >A a1;
- > >A a2;
- > >A a3;
- > >A a4;
- > >A a5;
- > >};
-
- > >class c1 : public C
- > >{
- > >b1 b1;
- > >b2 b2;
- > >};
-
- > >The question is, how within functions for class B do I access fields
- > >within class C, and similarly, how within the class A functions do I
- > >access fields within class B?
-
-
- > You have encountered the wall of encapsulation. It is considered poor
- > form to violate this wall without good reason. The need to violate this
-
- > wall may indicate poor design. However, C++ is not so rigid as to
- > disallow this. The way this is done is to have the class whose members
- > need to be accessed declare the class who wants to access them a friend:
-
- > class C
- > {
- > friend class B;
- > // ...
- > };
-
- > class B
- > {
- > friend class A;
- > // ...
- > };
-
- I obviously have not explained myself very well.
-
- Maybe instead of *access* I should have said identify.
-
- If, for example, B is
-
- class B
- {
- friend class A;
- int fred;
- }
-
- I cannot just
-
- A::A() {fred=21;};
-
- or even
-
- A::A() {B::fred=21;};
-
- The As are only members of a class that has B as a base.
-
- I suppose at its simplest level the problem is this
-
- class B
- {
- int fred;
- class A
- {
- A() { };
- };
- A a1;
- A a2;
- };
-
- How within A::A do I refer to fred.
-
- --
- Dave Mullard <dmullard@argonet.co.uk>
-
-